﻿using Furion.DatabaseAccessor;
using Furion.DataEncryption;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ST.Torch.WebApi.Core.Entities.System
{
    public enum SexEnum
    {
        /// <summary>
        /// 男
        /// </summary>
        Man,
        /// <summary>
        /// 女
        /// </summary>
        Woman
    }
    public class SYSUser : SoftDeleteBaseEntity, IEntityTypeBuilder<SYSUser>
    {
        /// <summary>
		/// 账号
		/// </summary>
		[MaxLength(100)]
        public string Account { get; set; }
        /// <summary>
		/// 密码
		/// </summary>
		[MaxLength(100)]
        public string Password { get; set; }

        /// <summary>
		/// 昵称
		/// </summary>
		[MaxLength(200)]
        public string NickName { get; set; }
        /// <summary>
		/// 图片
		/// </summary>
		[MaxLength(200)]
        public string Img { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public SexEnum Sex { get; set; }

         /// <summary>
         /// 联系方式
         /// </summary>
		[MaxLength(200)]
        public string Contact { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnable { get; set; }

        public List<SYSRole> Roles { get; set; }
        /// <summary>
        /// 密码加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string GetPassEncryption(string password)
        {
            return    MD5Encryption.Encrypt(password);
        }

        public void Configure(EntityTypeBuilder<SYSUser> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasKey(u => u.Id);
            entityBuilder.HasQueryFilter(u => !u.IsDeleted);
            entityBuilder.HasMany(u => u.Roles).WithMany(u => u.Users).UsingEntity(e => e.ToTable("SYSRoleUser"));


        }
    }
}
